<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=9; IE=8; IE=7; IE=EDGE">
    <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7"/>
    <title>乐优商城--购物车页面</title>

    <link rel="stylesheet" type="text/css" href="css/webbase.css"/>
    <link rel="stylesheet" type="text/css" href="css/pages-cart.css"/>
</head>

<body>

<div id="cartApp">

    <div class="top">
        <shortcut/>
    </div>

    <div class="cart py-container">

        <!--logoArea-->
        <div class="logoArea">
            <div class="fl logo"><span class="title">购物车</span></div>
        </div>

        <!--All goods-->
        <div class="allgoods">
            <h4>全部商品<span>11</span></h4>
            <div class="cart-main">
                <div class="yui3-g cart-th">
                    <div class="yui3-u-1-4"><input type="checkbox"/> 全部</div>
                    <div class="yui3-u-1-4">商品</div>
                    <div class="yui3-u-1-8">单价（元）</div>
                    <div class="yui3-u-1-8">数量</div>
                    <div class="yui3-u-1-8">小计（元）</div>
                    <div class="yui3-u-1-8">操作</div>
                </div>
                <div class="cart-item-list">

                    <div class="cart-body">
                        <div class="cart-list">
                            <ul class="goods-list yui3-g" v-for="cart,index in carts" :key="index">
                                <li class="yui3-u-1-24">
                                    <input type="checkbox" name="" :value="cart" v-model="selected"/>
                                </li>
                                <li class="yui3-u-11-24">
                                    <div class="good-item">
                                        <div class="item-img">
                                            <img :src="cart.image" width="80px" height="80px"/>
                                        </div>
                                        <div class="item-msg">
												<span style="line-height:70px ">
													{{cart.title}}<br/>

                                                    <span v-for="value,key in cart.ownSpec">
                                                       {{key}}:{{value}}
                                                    </span>

												</span>
                                        </div>
                                    </div>
                                </li>

                                <li class="yui3-u-1-8"><span style="line-height:70px " class="price" v-text="ly.formatPrice(cart.price)">8848.00</span></li>
                                <li class="yui3-u-1-8" style="padding-top: 20px">
                                    <a href="javascript:void(0)" class="increment mins" @click.prevent="decrement(cart)">-</a>
                                    <input autocomplete="off" type="text" value="1" minnum="1" class="itxt" v-model="cart.num"/>
                                    <a href="javascript:void(0)" class="increment plus" @click.prevent="increment(cart)">+</a>
                                </li>
                                <li class="yui3-u-1-8"><span style="line-height:70px " class="sum" v-text="ly.formatPrice(cart.price*cart.num)">8848.00</span></li>
                                <li class="yui3-u-1-8">
                                    <a href="#none" @click.prevent="deleteCart(cart,index)">删除</a><br/>
                                    <a href="#none">移到我的关注</a>
                                </li>
                            </ul>
                        </div>
                    </div>
                </div>

            </div>
            <div class="cart-tool">
                <div class="select-all">
                    <input type="checkbox" name="" id="" value="" v-model="selectedAll"/>
                    <span>全选</span>
                </div>
                <div class="option">
                    <a href="#none">删除选中的商品</a>
                    <a href="#none">移到我的关注</a>
                    <a href="#none">清除下柜商品</a>
                </div>
                <div class="toolbar">
                    <div class="chosed">已选择<span v-text="selected.length">0</span>件商品</div>
                    <div class="sumprice">
                        <span><em>总价（不含运费） ：</em><i class="summoney" v-text="ly.formatPrice(totalPrice)">8848.00</i></span>
                        <span><em>已节省：</em><i>-¥20.00</i></span>
                    </div>
                    <div class="sumbtn">
                        <a class="sum-btn" href="getOrderInfo.html" target="_blank" @click.prevent="toOrderInfo">结算</a>
                    </div>
                </div>
            </div>
            <div class="clearfix"></div>
            <div class="deled">
                <span>已删除商品，您可以重新购买或加关注：</span>
                <div class="cart-list del">
                    <ul class="goods-list yui3-g">
                        <li class="yui3-u-1-2">
                            <div class="good-item">
                                <div class="item-msg">Apple Macbook Air 13.3英寸笔记本电脑 银色（Corei5）处理器/8GB内存</div>
                            </div>
                        </li>
                        <li class="yui3-u-1-6"><span class="price">8848.00</span></li>
                        <li class="yui3-u-1-6">
                            <span class="number">1</span>
                        </li>
                        <li class="yui3-u-1-8">
                            <a href="#none">重新购买</a>
                            <a href="#none">移到我的关注</a>
                        </li>
                    </ul>
                </div>
            </div>
            <div class="liked">
                <ul class="sui-nav nav-tabs">
                    <li class="active">
                        <a href="#index" data-toggle="tab">猜你喜欢</a>
                    </li>
                    <li>
                        <a href="#profile" data-toggle="tab">特惠换购</a>
                    </li>
                </ul>
                <div class="clearfix"></div>
                <div class="tab-content">
                    <div id="index" class="tab-pane active">
                        <div id="myCarousel" data-ride="carousel" data-interval="4000" class="sui-carousel slide">
                            <div class="carousel-inner">
                                <div class="active item">
                                    <ul>
                                        <li>
                                            <img src="img/like1.png"/>
                                            <div class="intro">
                                                <i>Apple苹果iPhone 6s (A1699)</i>
                                            </div>
                                            <div class="money">
                                                <span>$29.00</span>
                                            </div>
                                            <div class="incar">
                                                <a href="#" class="sui-btn btn-bordered btn-xlarge btn-default"><i
                                                        class="car"></i><span class="cartxt">加入购物车</span></a>
                                            </div>
                                        </li>
                                        <li>
                                            <img src="img/like2.png"/>
                                            <div class="intro">
                                                <i>Apple苹果iPhone 6s (A1699)</i>
                                            </div>
                                            <div class="money">
                                                <span>$29.00</span>
                                            </div>
                                            <div class="incar">
                                                <a href="#" class="sui-btn btn-bordered btn-xlarge btn-default"><i
                                                        class="car"></i><span class="cartxt">加入购物车</span></a>
                                            </div>
                                        </li>
                                        <li>
                                            <img src="img/like3.png"/>
                                            <div class="intro">
                                                <i>Apple苹果iPhone 6s (A1699)</i>
                                            </div>
                                            <div class="money">
                                                <span>$29.00</span>
                                            </div>
                                            <div class="incar">
                                                <a href="#" class="sui-btn btn-bordered btn-xlarge btn-default"><i
                                                        class="car"></i><span class="cartxt">加入购物车</span></a>
                                            </div>
                                        </li>
                                        <li>
                                            <img src="img/like4.png"/>
                                            <div class="intro">
                                                <i>Apple苹果iPhone 6s (A1699)</i>
                                            </div>
                                            <div class="money">
                                                <span>$29.00</span>
                                            </div>
                                            <div class="incar">
                                                <a href="#" class="sui-btn btn-bordered btn-xlarge btn-default"><i
                                                        class="car"></i><span class="cartxt">加入购物车</span></a>
                                            </div>
                                        </li>
                                    </ul>
                                </div>
                                <div class="item">
                                    <ul>
                                        <li>
                                            <img src="img/like1.png"/>
                                            <div class="intro">
                                                <i>Apple苹果iPhone 6s (A1699)</i>
                                            </div>
                                            <div class="money">
                                                <span>$29.00</span>
                                            </div>
                                            <div class="incar">
                                                <a href="#" class="sui-btn btn-bordered btn-xlarge btn-default"><i
                                                        class="car"></i><span class="cartxt">加入购物车</span></a>
                                            </div>
                                        </li>
                                        <li>
                                            <img src="img/like2.png"/>
                                            <div class="intro">
                                                <i>Apple苹果iPhone 6s (A1699)</i>
                                            </div>
                                            <div class="money">
                                                <span>$29.00</span>
                                            </div>
                                            <div class="incar">
                                                <a href="#" class="sui-btn btn-bordered btn-xlarge btn-default"><i
                                                        class="car"></i><span class="cartxt">加入购物车</span></a>
                                            </div>
                                        </li>
                                        <li>
                                            <img src="img/like3.png"/>
                                            <div class="intro">
                                                <i>Apple苹果iPhone 6s (A1699)</i>
                                            </div>
                                            <div class="money">
                                                <span>$29.00</span>
                                            </div>
                                            <div class="incar">
                                                <a href="#" class="sui-btn btn-bordered btn-xlarge btn-default"><i
                                                        class="car"></i><span class="cartxt">加入购物车</span></a>
                                            </div>
                                        </li>
                                        <li>
                                            <img src="img/like4.png"/>
                                            <div class="intro">
                                                <i>Apple苹果iPhone 6s (A1699)</i>
                                            </div>
                                            <div class="money">
                                                <span>$29.00</span>
                                            </div>
                                            <div class="incar">
                                                <a href="#" class="sui-btn btn-bordered btn-xlarge btn-default"><i
                                                        class="car"></i><span class="cartxt">加入购物车</span></a>
                                            </div>
                                        </li>
                                    </ul>
                                </div>
                            </div>
                            <a href="#myCarousel" data-slide="prev" class="carousel-control left">‹</a>
                            <a href="#myCarousel" data-slide="next" class="carousel-control right">›</a>
                        </div>
                    </div>
                    <div id="profile" class="tab-pane">
                        <p>特惠选购</p>
                    </div>
                </div>
            </div>
        </div>
    </div>

</div>
<script src="./js/vue/vue.js"></script>
<script src="./js/axios.min.js"></script>
<script src="./js/common.js"></script>
<script type="text/javascript">
    var cartVm = new Vue({
        el: "#cartApp",
        data: {
            ly,
            carts:[], //页面中要展示的所有的商品数据
            selected:[],
            selectedAll:false
        },
        components: {
            shortcut: () => import("/js/pages/shortcut.js")
        },
        created() {//用来加载页面要展示的数据，登录了去服务器，没有登录在客户端本地

            this.loadCarts();
        },
        watch:{
            selected:{//监控选中的数组，如果其变化了，看当前选中的数组长度是否等于carts的长度,如果想等全选就为真
                deep:true,
                handler(){
                    this.selectedAll = this.selected.length===this.carts.length
                }

            },
            selectedAll(val){//监控全选框的值，如果为true，则实现全选
                if (val) {
                    this.selected = this.carts;
                }
            }
        },

        methods: {
            loadCarts() {
                // 先判断登录状态
                ly.verifyUser()
                    .then(() => {
                        // 已登录

                        //发起请求，查询后台的购物车信息
                        //TODO 在发起请求之前，判断localStorage中是否有数据，如果有数据需要先把数据提交到后台服务器
                        //然后删除localStorage中的信息
                        ly.http.get("cart")
                            .then(resp=>{
                                //请求成功，返回购物车信息

                                this.carts = resp.data;

                                //把当年的ownSpec从string转为对象
                                this.carts.forEach(ct=>ct.ownSpec = ly.parse(ct.ownSpec))


                                //默认全选
                                this.selected = this.carts;

                                this.selectedAll = true;

                            }).catch(resp=>{
                            console.log("暂无查到")
                        })

                    })
                    .catch(() => {
                        // 未登录  从本地localStorage中取出localCarts，如果没有赋值空数组
                        this.carts = ly.store.get("localCarts") || [];

                        //默认全选
                        this.selected = this.carts;

                        this.selectedAll = true;
                    })
            },

            deleteCart(cart,index){
                ly.verifyUser()
                    .then(resp=>{
                        //登录成功
                        //需要把当前skuId传递到后台
                        ly.http.delete("cart/"+cart.skuId)
                            .then(resp=>{
                                //删除成功
                                //TODO VUE重新渲染

                                this.carts.splice(index,1);

                            }).catch(resp=>{
                                console.log("删除失败");
                        })



                    }).catch(resp=>{
                        //没有登录
                    let localCarts = ly.store.get("localCarts") || [];

                    if (localCarts) {


                        //指定位置删除
                        localCarts.splice(index,1);

                        //把删除后的结果保存到localStorage中
                        ly.store.set("localCarts",localCarts);

                        //TODO 要更新页面数据，可以直接刷新，我们拒绝
                        this.carts = localCarts;

                        //②在carts,数组中直接根据index位置删除

                    }
                })
            },
            //购物车展示页面的商品数量的修改
            increment(cart){

                ly.verifyUser()
                    .then(resp=>{
                        //已经登录
                        ly.http.put("cart/increment/"+cart.skuId)
                            .then(resp=>{
                                //修改成功后，页面也要同步更新
                                cart.num++;
                            }).catch(resp=>{

                        })



                    }).catch(resp=>{
                        //还未登录
                    let localCarts = ly.store.get("localCarts") || [];

                    if (localCarts) {
                        //遍历数组根据id查询出要改的那个对象
                        let localCart = localCarts.find(lct=>lct.skuId===cart.skuId);
                        localCart.num++;

                        //要把加完的结果重新保存
                        ly.store.set("localCarts",localCarts);

                        //更新vue
                        this.carts = localCarts;

                    }
                })

            },
            toOrderInfo(){



                // 判断是否登录
                ly.http.get("/auth/verify")
                    .then(() => {
                        // 已登录
                        //隐士传参
                        //localStorage

                        ly.store.set("toBeBuy",this.selected);

                        window.location.href="http://www.leyou.com/getOrderInfo.html";
                    }).catch(() => {
                    // 未登录
                    window.location.href = "/login.html?returnUrl=" + window.location.href;
                })


            }
        },
        computed: {
            totalPrice() {
                return this.selected.map(c => c.num * c.price).reduce((p1, p2) => p1 + p2, 0);
            }
        }
    })
</script>
<!-- 底部栏位 -->
<!--页面底部，由js动态加载-->
<script type="text/javascript" src="js/plugins/jquery/jquery.min.js"></script>
<div class="clearfix footer"></div>
<script type="text/javascript">$(".footer").load("foot.html");</script>
<!--页面底部END-->
<script type="text/javascript" src="js/plugins/jquery.easing/jquery.easing.min.js"></script>
<script type="text/javascript" src="js/plugins/sui/sui.min.js"></script>
<script type="text/javascript" src="js/widget/nav.js"></script>

</body>

</html>